//登录窗口
package cn.the.angry.login;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.IOException;
import java.net.BindException;
import java.net.DatagramSocket;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.json.JSONObject;

import cn.the.angry.MainTest;
import cn.the.angry.resourcemanager.ResourcePool;
import cn.the.angry.tests.SimulativeTest;

public class LoginFrame extends JFrame{
	
	private static final int WIDTH = 260;
	private static final int HEIGHT = 190;
	
	private static JTextField jText_user = new JTextField();
	private static JTextField jText_pass = new JPasswordField();
	static {
		
	}
	private static JLabel jLabel_user = new JLabel();
	private static JLabel jLabel_pass = new JLabel();
	private static JButton jButton = new JButton(){
		@Override
		public void paint(Graphics g){
			g.drawImage(ResourcePool.getLoginButtonImage(), 0, 0 , 90, 40, null);
		}
	};
	private MySocketController mySocketController;
		
	public LoginFrame () {
		super();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(WIDTH, HEIGHT);
		this.setResizable(false);
		this.setUndecorated(true);
		this.setLocationRelativeTo(null);
		try {
			int ipAddress=5199;
			do
			{
				try
				{
					mySocketController = new MySocketController(new DatagramSocket(ipAddress));
				}
				catch(final BindException e)
				{
					++ipAddress;
					continue;
				}
				break;
			}
			while(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		addComponents(this.getContentPane());
		new Thread()
		{
			@Override
			public void run()
			{
				try
				{
					Thread.sleep(50);
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
				jText_user.requestFocus();
			};
		}.start();
	}
	
	private void addComponents(Container con){
		jLabel_user.setBounds(10, 30, 80, 30);
		jLabel_user.setText("User: ");
		jLabel_pass.setBounds(10, 80, 80, 30);
		jLabel_pass.setText("Pass: ");
		jText_user.setBounds(80, 30, 160, 30);
		jText_pass.setBounds(80, 80, 160, 30);
		jButton.setBounds(150, 130, 90, 40);
		
		jButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try
				{
					if(verified()){
						final MainTest main = new MainTest();
					    main.setVisible(true);
					    MainTest.bindMouseEvents(main);
					    main.startTest();
					    MainTest.loopMusic("/res/raw/ambient_green_jungleish.mp3", 0);
					    new Thread(){
					    	@Override
					    	public void run(){
					    		try {
					    			LoginFrame.this.dispose();
								} catch (Exception e) {
								}
					    	}
					    }.start();
					}else{
						getError("用户名或密码错误");
					}
				}
				catch (SocketTimeoutException exception)
				{
				}
				
			}
		});
		
		jText_user.addActionListener(new ActionListener()
		{			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				jText_pass.requestFocus();
			}
		});
		
		jText_pass.addActionListener(new ActionListener()
		{			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				jButton.doClick();
			}
		});
		
		con.add(jLabel_user,null);
		con.add(jLabel_pass,null);
		con.add(jText_user,null);
		con.add(jText_pass,null);
		con.add(jButton,null);
		con.add(background, null);
	}
	
	private final JPanel background = new JPanel(){
		@Override
		public void paint(Graphics g){
			g.drawImage(ResourcePool.getLoginBackgroundImage(), 0, 0 , LoginFrame.WIDTH, LoginFrame.HEIGHT, null);
			g.dispose();
		}
	};
	
	public boolean verified() throws SocketTimeoutException{
		Map<String, String> info = new HashMap<String, String>();
		info.put("operation", "login");
		info.put("username", getUsername());
		info.put("password", getPassword());
		JSONObject request = new JSONObject(info);
		try
		{
			mySocketController.setContent(request.toString(), 5188, "127.0.0.1");
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
		try 
		{
			String answer = mySocketController.getContent(1000);
			if(answer.equals("true")) return true;
		} 
		catch (SocketTimeoutException e)
		{
			getError("连接服务器超时");
			throw e;
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		
		return false;
	}
	
	
	public String getUsername(){
		return jText_user.getText();
	}
		
	public String getPassword(){
		return jText_pass.getText();
	}
	
	
	public static void getError(String s){
		JOptionPane.showMessageDialog(null,s,
		"警告", JOptionPane.ERROR_MESSAGE);
	}
}

